using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Colman.Rpg.Controls
{
    /// <summary>
    /// The control that shows the stock for either the player or the store.
    /// </summary>
    [DefaultEvent("SelectedItemChanged")]
    public partial class StockControl : UserControl
    {
        /// <summary>
        /// Creates a new instance of <see cref="StockControl" />.
        /// </summary>
        public StockControl()
        {
            InitializeComponent();

            this.dgvWeapons.AutoGenerateColumns = false;
            this.dgvArmors.AutoGenerateColumns = false;
            this.dgvMagicItems.AutoGenerateColumns = false;
        }

        #region List<Item> DataSource { get; set; } (dataSource)
        private List<Item> dataSource;

        /// <summary>
        /// Gets or sets the character's defense points.
        /// </summary>
        public List<Item> DataSource
        {
            [System.Diagnostics.DebuggerStepThrough]
            get
            {
                return this.dataSource;
            }
            set
            {
                if (this.dataSource != value)
                {
                    this.dataSource = value;

                    if (this.dataSource != null)
                    {
                        this.dgvWeapons.DataSource = this.dataSource.FindAll(delegate(Item item) { return item is Weapon; });
                        this.dgvArmors.DataSource = this.dataSource.FindAll(delegate(Item item) { return item is Armor; });
                        this.dgvMagicItems.DataSource = this.dataSource.FindAll(delegate(Item item) { return item is MagicItem; });
                    }

                    this.RefreshSelectedItem();
                }
            }
        }
        #endregion

        #region Item SelectedItem { get; set; } (selectedItem) + SelectedItemChanged
        private Item selectedItem;

        /// <summary>
        /// Gets or sets the currently selected item in the control.
        /// </summary>
        /// <remarks>Raises the <see cref="SelectedItemChanged"/> event when set.</remarks>
        [System.ComponentModel.Description("Indicates the currently selected item in the control.")]
        [Browsable(false)]
        public Item SelectedItem
        {
            [System.Diagnostics.DebuggerStepThrough]
            get
            {
                return this.selectedItem;
            }
            [System.Diagnostics.DebuggerStepThrough]
            set
            {
                if (this.selectedItem != value)
                {
                    this.selectedItem = value;

                    this.OnSelectedItemChanged(EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Invokes the <see cref="SelectedItemChanged" /> event.
        /// </summary>
        /// <param name="e">The event's arguments.</param>
        protected virtual void OnSelectedItemChanged(EventArgs e)
        {
            if (this.SelectedItemChanged != null)
                this.SelectedItemChanged(this, e);
        }

        /// <summary>
        /// Raised when the <see cref="SelectedItem"/> property is changed.
        /// </summary>
        public event EventHandler SelectedItemChanged;
        #endregion

        #region bool ShowAmounts { get; set; } (showAmounts)
        private bool showAmounts = true;

        /// <summary>
        /// Gets or sets whether to show the amounts of items.
        /// </summary>
        [System.ComponentModel.Description("Indicates whether to show the amounts of items.")]
        [DefaultValue(true)]
        public bool ShowAmounts
        {
            [System.Diagnostics.DebuggerStepThrough]
            get
            {
                return this.showAmounts;
            }
            set
            {
                if (this.showAmounts != value)
                {
                    this.showAmounts = value;

                    this.colWeaponAmount.Visible = value;
                    this.colArmorAmount.Visible = value;
                    this.colMIAmount.Visible = value;
                }
            }
        }
        #endregion

        private void dgvWeapons_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.SelectedItem = ((List<Item>)(this.dgvWeapons.DataSource))[e.RowIndex];
        }

        private void dgvArmors_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.SelectedItem = ((List<Item>)(this.dgvArmors.DataSource))[e.RowIndex];
        }

        private void dgvMagicItems_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.SelectedItem = ((List<Item>)(this.dgvMagicItems.DataSource))[e.RowIndex];
        }

        private void itemTabs_Selected(object sender, TabControlEventArgs e)
        {
            this.RefreshSelectedItem();
        }

        private void RefreshSelectedItem()
        {
            if (this.itemTabs.SelectedTab == this.tpWeapons)
            {
                if (this.dgvWeapons.CurrentRow != null)
                {
                    this.SelectedItem = ((List<Item>)(this.dgvWeapons.DataSource))[this.dgvWeapons.CurrentRow.Index];
                }
                else
                {
                    this.SelectedItem = null;
                }
            }
            else if (this.itemTabs.SelectedTab == this.tpArmors)
            {
                if (this.dgvArmors.CurrentRow != null)
                {
                    this.SelectedItem = ((List<Item>)(this.dgvArmors.DataSource))[this.dgvArmors.CurrentRow.Index];
                }
                else
                {
                    this.SelectedItem = null;
                }
            }
            else
            {
                if (this.dgvMagicItems.CurrentRow != null)
                {
                    this.SelectedItem = ((List<Item>)(this.dgvMagicItems.DataSource))[this.dgvMagicItems.CurrentRow.Index];
                }
                else
                {
                    this.SelectedItem = null;
                }
            }
        }
    }
}